// 2023-03-20 16:42:32

class TreeNode {
    /**
     * @param {string} value 节点值
     * @param {TreeNode} left 
     * @param {TreeNode} right 
     */
    constructor(value, left, right) {
        this.value = (value === undefined ? 0 : value);
        this.left = (left === undefined ? null : left);
        this.right = (right === undefined ? null : right);
    }
    /**
     * 广序生成二叉树
     * @param {Array} array 
     */
    static CreateTree(array) {
        if (array.length < 1) return null;
        let root = new TreeNode(array[0]);
        //使用队列保证广序节点顺序
        const queue = [];
        queue.push(root);
        let current = root;
        let node;
        for (let i = 1; i < array.length; i++) {
            if (array[i]) {
                node = new TreeNode(array[i]);
            } else node = null
            if (i % 2 == 1) {
                current = queue.shift();
                current.left = node;
            } else {
                current.right = node;
            }
            queue.push(node);
        }
        return root;
    }
}

/**
 * 定义WebSocket发送信息的数据类型
 */
class WebSocketData {
    /**
     * 定义数据WebSocket传输的数类型
     * @param {string} data A valid JSON {msg:信息载体}
     * @param { {_initInfo:Boolean,sendID:string,receiverID:string} | { _initInfo: Boolean,userID:String }} options
     * @returns {{data:any,headers:{sendID:string,receiverID:string,authorization:string}}}
     */
    constructor(data, options = {}) {
        try {
            this.data = {}
            if (options._initInfo) {
                if (options.userID) {
                    this.headers['userID'] = options.userID
                } else throw TypeError('userID不能为空')
            } else {
                if (options.receiverID && options.sendID) {
                    this.data.msg = JSON.parse(data)
                    this.headers['sendID'] = options.sendID
                    this.headers['receiverID'] = options.receiverID
                    // 携带token
                    let info = window.localStorage.getItem('huily')
                    let token = JSON.parse(info).token ? JSON.parse(info).token : 'token'
                    this.headers['authorization'] = `huily ${token}`
                } else throw TypeError('sendID或receiverID不能为空')
            }
        } catch (error) {
            console.error(error);
        }
    }
}

class ForestNode {
    constructor(data) {
        if (data) {
            for (const key in data) {
                if (Object.hasOwnProperty.call(data, key)) {
                    const element = data[key];
                    this[key] = element
                }
            }
        } else this.id = null
        this.children = []
    }
}
// 森林
class ForestTree {
    /**
     * 
     * @param {Array<{parent:string,content:any}>} data 
     */
    constructor(data) {
        for (const key in data) {
            if (Object.hasOwnProperty.call(data, key)) {
                const element = data[key];
                this[key] = element
            }
        }
        this.children = ForestTree.ToForest(data)
    }
    /**
     * 有root节点实现方式
     * @param {Array<{id:string,parentID:string}>} data
     * data 一定要有parent值和value值
     * 约定森林根节点parent为‘root’
     */
    static ToForest(data, parentID = null, order = false) {
        // 先生成一个空值的根节点
        if (!data || !data.length) return
        let collect = []
        data.forEach(item => {
            if (item.parentID == parentID) {
                let node = new ForestNode(item)
                node.children = this.ToForest(data, node.id)
                collect.push(node)
            }
        })
        return collect
    }
}

export {
    WebSocketData,
    ForestNode,
    ForestTree
}